using EasyAdmin.Admin.Application.Services;
using EasyAdmin.Admin.Entity.Entities;
using EasyAdmin.Admin.Entity.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EasyAdmin.Admin.WebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class SysRoleController : ControllerBase
    {
        private readonly ISysRoleService _roleService;

        public SysRoleController(ISysRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        [HttpGet("list")]
        public async Task<ApiResult<object>> GetRoleList([FromQuery] string keyword)
        {
            var roles = await _roleService.GetRoleListAsync(keyword);
            return ApiResult<object>.Ok(roles);
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        [HttpGet("{id}")]
        public async Task<ApiResult<SysRole>> GetRole(int id)
        {
            var role = await _roleService.GetRoleByIdAsync(id);
            if (role == null)
                return ApiResult<SysRole>.NotFound("角色不存在");

            return ApiResult<SysRole>.Ok(role);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        [HttpPost]
        public async Task<ApiResult> CreateRole([FromBody] SysRole role)
        {
            var result = await _roleService.CreateRoleAsync(role);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        [HttpPut("{id}")]
        public async Task<ApiResult> UpdateRole(int id, [FromBody] SysRole role)
        {
            if (id != role.Id)
                return ApiResult.BadRequest("角色ID不匹配");

            var result = await _roleService.UpdateRoleAsync(role);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<ApiResult> DeleteRole(int id)
        {
            var result = await _roleService.DeleteRoleAsync(id);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 设置角色状态
        /// </summary>
        [HttpPut("{id}/status/{status}")]
        public async Task<ApiResult> SetRoleStatus(int id, int status)
        {
            var result = await _roleService.SetRoleStatusAsync(id, status);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 分配角色权限
        /// </summary>
        [HttpPost("{id}/permissions")]
        public async Task<ApiResult> AssignPermissions(int id, [FromBody] List<int> menuIds)
        {
            var result = await _roleService.AssignPermissionsAsync(id, menuIds);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 获取角色权限ID列表
        /// </summary>
        [HttpGet("{id}/permissions")]
        public async Task<ApiResult<object>> GetRolePermissions(int id)
        {
            var menuIds = await _roleService.GetRolePermissionsAsync(id);
            return ApiResult<object>.Ok(menuIds);
        }
    }
}